package com.originskyseed.platform.user.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Component;

import com.originskyseed.platform.core.base.BasePage;
import com.originskyseed.platform.core.tenant.util.TenantCodeUtil;
import com.originskyseed.platform.core.util.bean.PrimaryKeyGeneratorUtil;
import com.originskyseed.platform.user.mapper.AnchorRelateLiveMapper;
import com.originskyseed.platform.user.model.AnchorProfitVO;
import com.originskyseed.platform.user.model.AnchorRelateLive;
import com.originskyseed.platform.user.service.IAnchorRelateLiveService;

/**
 * 主播直播信息表Service接口实现类（OSI_ANCHOR_RELATE_LIVE）
 *
 * @author Tangwanghui
 */
@DubboService
@Component("anchorRelateLiveService")
public class AnchorRelateLiveServiceImpl implements IAnchorRelateLiveService {

	@Resource
	private AnchorRelateLiveMapper anchorRelateLiveMapper;

	/**
	 * 删除记录
	 *
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@Override
	public int deleteByPrimaryKey(String id) throws Exception {
		return this.anchorRelateLiveMapper.deleteByPrimaryKey(id);
	}

	/**
	 * 批量删除记录
	 *
	 * @param ids
	 * @return
	 * @throws Exception
	 */
	@Override
	public int deleteByPrimaryKeys(List<String> ids) throws Exception {
		return this.anchorRelateLiveMapper.deleteByPrimaryKeys(ids);
	}

	/**
	 * 插入记录
	 *
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public int insert(AnchorRelateLive record) throws Exception {
		if (StringUtils.isBlank(record.getId())) {
			record.setId(PrimaryKeyGeneratorUtil.getInstance().getRandomKey());
		}
		record.setCreateTime(new Date());
		return this.anchorRelateLiveMapper.insert(record);
	}

	/**
	 * 插入记录（仅非空值字段）
	 *
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public int insertSelective(AnchorRelateLive record) throws Exception {
		if (StringUtils.isBlank(record.getId())) {
			record.setId(PrimaryKeyGeneratorUtil.getInstance().getRandomKey());
		}
		record.setCreateTime(new Date());
		return this.anchorRelateLiveMapper.insertSelective(record);
	}

	/**
	 * 更新记录
	 *
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public int updateByPrimaryKey(AnchorRelateLive record) throws Exception {
		record.setUpdateTime(new Date());
		return this.anchorRelateLiveMapper.updateByPrimaryKey(record);
	}

	/**
	 * 更新记录（仅非空值字段）
	 *
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public int updateByPrimaryKeySelective(AnchorRelateLive record) throws Exception {
		record.setUpdateTime(new Date());
		return this.anchorRelateLiveMapper.updateByPrimaryKeySelective(record);
	}

	/**
	 * 查询记录
	 *
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@Override
	public AnchorRelateLive selectByPrimaryKey(String id) throws Exception {
		return this.anchorRelateLiveMapper.selectByPrimaryKey(id);
	}

	/**
	 * 查询列表
	 *
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public List<AnchorRelateLive> selectList(AnchorRelateLive record) throws Exception {
		return this.anchorRelateLiveMapper.selectList(record);
	}

	/**
	 * 主播id
	 *
	 * @param liveId
	 * @return
	 * @throws Exception
	 */
	public AnchorRelateLive selectByLiveId(String tenantCode,String liveId) throws Exception{
		if(tenantCode!=null&&StringUtils.isNotBlank(tenantCode))
		{
			TenantCodeUtil.getInstance().createServerTenantCode(tenantCode);
		}
		return selectByLiveId(liveId);
	}


	/**
	 * 主播id
	 *
	 * @param liveId
	 * @return
	 * @throws Exception
	 */
	public AnchorRelateLive selectByLiveId(String liveId) throws Exception
	{
		return this.anchorRelateLiveMapper.selectByLiveId(liveId);
	}

	/**
	 * 分页查询列表
	 *
	 * @param params
	 * @param page
	 * @return
	 * @throws Exception
	 */
	@Override
	public BasePage<AnchorRelateLive> selectListByPage(Map<String, Object> params, BasePage<AnchorRelateLive> page) throws Exception {
		params.put("page", page);
		List<AnchorRelateLive> list = this.anchorRelateLiveMapper.selectListByPage(params);
		page.setResults(list);
		return page;
	}

	/**
	 * 批量插入记录
	 *
	 * @param list
	 * @return
	 * @throws Exception
	 */
	public int insertBatch(List<AnchorRelateLive> list) throws Exception {
		return this.anchorRelateLiveMapper.insertBatch(list);
	}


	/**
	 * 批量插入记录
	 *
	 * @param list
	 * @return
	 * @throws Exception
	 */
	public int insertBatch(String tenantCode,List<AnchorRelateLive> list) throws Exception {
		if(tenantCode!=null&&StringUtils.isNotBlank(tenantCode))
		{
			TenantCodeUtil.getInstance().createServerTenantCode(tenantCode);
		}
		return this.anchorRelateLiveMapper.insertBatch(list);
	}

	/**
	 * 统计主播直播时长
	 *
	 * @param map
	 * @return
	 * @throws Exception
	 */
	@Override
	public Long countAnchorLiveTime(Map<String, Object> map) throws Exception {
		return this.anchorRelateLiveMapper.countAnchorLiveTime(map);
	}

	/**
	 * pk统计主播直播时长
	 *
	 * @param map
	 * @return
	 * @throws Exception
	 */
	@Override
	public Long pkCountAnchorLiveTimeByIds(Map<String, Object> map) throws Exception {
		return this.anchorRelateLiveMapper.pkCountAnchorLiveTimeByIds(map);
	}

	/**
	 * 统计主播直播时长(租户模式)
	 *
	 * @param map
	 * @param tenantCode
	 * @return
	 * @throws Exception
	 */
	@Override
	public Long pkCountAnchorLiveTimeTenantCode(Map<String, Object> map, String tenantCode) throws Exception {
		if (StringUtils.isNotEmpty(tenantCode)) {
			TenantCodeUtil.getInstance().createServerTenantCode(tenantCode);
		}
		return this.pkCountAnchorLiveTimeByIds(map);
	}


	/**
	 * 批量插入记录
	 *
	 * @param anchorRelateLiveList
	 * @return
	 * @throws Exception
	 */
	public int insertBatch(String tenantCode,List<AnchorRelateLive> anchorRelateLiveList, List<AnchorRelateLive> updateList) throws Exception
	{
		if (StringUtils.isNotEmpty(tenantCode)) {
			TenantCodeUtil.getInstance().createServerTenantCode(tenantCode);
		}
		return insertBatch(anchorRelateLiveList,updateList);
	}

	/**
	 * 批量插入记录
	 *
	 * @param anchorRelateLiveList
	 * @return
	 * @throws Exception
	 */
	public int insertBatch(List<AnchorRelateLive> anchorRelateLiveList, List<AnchorRelateLive> updateList) throws Exception {
		for (AnchorRelateLive anchorRelateLive : updateList) {
			this.anchorRelateLiveMapper.updateByPrimaryKeySelective(anchorRelateLive);
		}
		if (anchorRelateLiveList.size() > 0) {
			this.anchorRelateLiveMapper.insertBatch(anchorRelateLiveList);
		}
		return 1;
	}

	/**
	 * 批量插入记录,月表
	 *
	 * @param anchorRelateLiveList
	 * @return
	 * @throws Exception
	 */
	public int insertMonthBatch(List<AnchorRelateLive> anchorRelateLiveList, List<AnchorRelateLive> updateList) throws Exception{
		for (AnchorRelateLive anchorRelateLive : updateList) {
			this.anchorRelateLiveMapper.updateMonthByPrimaryKeySelective(anchorRelateLive);
		}
		if (anchorRelateLiveList.size() > 0) {
			this.anchorRelateLiveMapper.insertMonthBatch(anchorRelateLiveList);
		}
		return 1;
	}


	/**
	 * 批量插入记录,月表
	 *
	 * @param anchorRelateLiveList
	 * @return
	 * @throws Exception
	 */
	public int insertMonthBatch(String tenantCode,List<AnchorRelateLive> anchorRelateLiveList, List<AnchorRelateLive> updateList) throws Exception
	{
		if (StringUtils.isNotEmpty(tenantCode)) {
			TenantCodeUtil.getInstance().createServerTenantCode(tenantCode);
		}
		return  insertMonthBatch(anchorRelateLiveList,updateList);
	}

	/**
	 * 分页查询主播收益
	 *
	 * @param params
	 * @param page
	 * @return
	 * @throws Exception
	 */
	@Override
	public BasePage<AnchorProfitVO> selectAnchorProfitByPage(Map<String, Object> params, BasePage<AnchorProfitVO> page) throws Exception {
		params.put("page", page);
		List<AnchorProfitVO> list = this.anchorRelateLiveMapper.selectAnchorProfitByPage(params);
		page.setResults(list);
		return page;
	}

	/**
	 * 查询有效天数
	 *
	 * @param map
	 * @return
	 * @throws Exception
	 */
	@Override
	public Integer selectValidDay(Map<String, Object> map) throws Exception {
		return this.anchorRelateLiveMapper.selectValidDay(map);
	}
}
